package com.sbl.bajw.pc.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.pc.model.LogTModel;
import com.sbl.bajw.pc.model.PersonnelRecordModel;
import com.sbl.bajw.pc.service.LogTModelService;
import com.sbl.bajw.pc.service.PersonnelRecordService;
import com.sbl.bajw.util.DateUtil;
import com.sbl.bajw.util.JsonUtil;

@Controller
@RequestMapping("/PersonnelRecord")
public class PersonnelRecordAction {

	@Resource
	private PersonnelRecordService service;
	
	@Resource
	private LogTModelService logService;
	
	/**
	 * @标题：createApp
	 * @作者：罗琦
	 * @时间：2015年6月5日
	 * @作用：APP人员登记数据入库
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/createApp")
	public @ResponseBody String createApp(HttpServletRequest request, HttpServletResponse response) throws Exception{
		PersonnelRecordModel model = RequestUnit.getModel(PersonnelRecordModel.class);
		LogTModel logT = new LogTModel();
		logT.setUserid(model.getCreater());
		logT.setCode("16");
		
		service.insert(model);
		logService.insert(logT);
		
		String json = JsonResultUtil.Success();
		return json;
	}
	
	/**
	 * @标题：getListApp
	 * @作者：罗琦
	 * @时间：2015年6月19日
	 * @作用：APP列表展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getListApp")
	public @ResponseBody String getListApp(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, String> parm = RequestUnit.getHashMapParmS();
		String start = DateUtil.getMondayDate();
		String stop = DateUtil.getSundayDate();
		parm.put("startdate", start + " 00:00:00");
		parm.put("stopdate", stop + "23:59:59");
		
		List<PersonnelRecordModel> list = service.getListApp(parm);
		int num = service.getCountApp(parm);
		String result = "{\"num\":" + num + ",\"list\":" + JsonUtil.ConvertArrayToJson(list) + "}";
		String json = JsonResultUtil.Success(JsonUtil.ConvertObjectToJson(result));
		return json;
	}
	
	/**
	 * @标题：detailApp
	 * @作者：罗琦
	 * @时间：2015年6月19日
	 * @作用：APP详情展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/detailApp")
	public @ResponseBody String detailApp(HttpServletRequest request, HttpServletResponse response){
		String personId = RequestUnit.getString("personId");
		PersonnelRecordModel model = service.getModelApp(personId);
		String json = JsonUtil.ConvertObjectToJson(model);
		return JsonResultUtil.Success(json);
	}
	
	/**
	 * @标题：getList
	 * @作者：罗琦
	 * @时间：2015年6月19日
	 * @作用：后台列表展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getList")
	public @ResponseBody String getList(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String result = service.getList(parm);
		return JsonUtil.ConvertObjectToJson(result);
	}
	
	@RequestMapping(value = "/List")
	public ModelAndView List(){
		ModelAndView mav = new ModelAndView("/view/manager/PersonnelRecord/List");
		return mav;
	}
	
	/**
	 * @标题：detail
	 * @作者：罗琦
	 * @时间：2015年6月19日
	 * @作用：后台详情展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/detail")
	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("id");
		PersonnelRecordModel model = service.getModel(id);
		List<String> list = new ArrayList<String>();
		if(model.getFile1() != null && !"".equals(model.getFile1())){
			list.add(model.getFile1());
		}
		if(model.getFile2() != null && !"".equals(model.getFile2())){
			list.add(model.getFile2());
		}
		if(model.getFile3() != null && !"".equals(model.getFile3())){
			list.add(model.getFile3());
		}
		if(model.getFile4() != null && !"".equals(model.getFile4())){
			list.add(model.getFile4());
		}
		request.setAttribute("model", model);
		request.setAttribute("pics", list);
		ModelAndView mav = new ModelAndView("/view/manager/PersonnelRecord/detail");
		return mav;
	}
}
